package net.easyjava.framework.third.aliyun;

import com.aliyun.api.domain.Job;
import com.taobao.api.ApiException;
import net.easyjava.framework.utils.ProjectProperties;
import net.easyjava.third.aliyun.AliyunConstants;
import net.easyjava.third.aliyun.trans.AliyunTransClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxnacy on 16/7/18.
 */
public class AliyunTransUtils {
    private static Logger logger = LoggerFactory.getLogger(AliyunTransUtils.class);
    static AliyunTransClient client;
    static String endpoint;
    static {
        if(client == null){
            init();
        }
    }
    private static synchronized void init(){
        if(client == null){
            ProjectProperties properties = ProjectProperties.getInstance();
            String key = properties.getProperty("aliyun.key.id");
            String secret = properties.getProperty("aliyun.key.secret");
            endpoint = properties.getProperty("aliyun.endpoint");
            try {
                client = AliyunTransClient.getInstance(key,secret);
            } catch (Exception e) {
                logger.error("AliyunOSSClient 初始化失败 key:{} secret:{}",key,secret);
                logger.error(e.getMessage(),e.fillInStackTrace());
            }
        }
    }

    /**
     * 提交北京转码任务
     * @param inBucket
     * @param inKey
     * @param outBucket
     * @param outKey
     * @param pipelinId
     * @param templateId
     * @return
     */
    public static Job submitBJJob(String inBucket,String inKey
            ,String outBucket,String outKey,String pipelinId,String templateId){
        Job job = null;
        try {
            job = client.submitBJJobs(inBucket, inKey, outBucket, outKey, pipelinId, templateId);

        } catch (Exception e) {
            logger.error("AliyunTransUtils 提交北京转码任务失败 inBucket:{} inKey:{} outBucket:{} outKey:{} pipelinId:{} templateId:{} "
                    ,inBucket,inKey,outBucket,outKey,pipelinId,templateId);
            logger.error(e.getMessage(),e.fillInStackTrace());e.printStackTrace();
        }
        return job;
    }


    /**
     * 查询北京转码任务
     * @param jobIds 一个或多个jobid  用逗号隔开
     * @return
     */
    public static List<Job> queryBJJobList(String jobIds){
        List<Job> jobs = new ArrayList<>();
        try {
            jobs = client.queryJobList(jobIds, AliyunConstants.BUCKET_LOCATION.BJ);

        } catch (Exception e) {
            logger.error("AliyunTransUtils 查询北京转码列表失败 jobIds:{}"
                    ,jobIds);
            logger.error(e.getMessage(),e.fillInStackTrace());e.printStackTrace();
        }
        return jobs;
    }











}
